//
//  WKWebView_Demo1VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/5/6.
//

import UIKit
import WebKit

class WKWebView_Demo1VC: UIViewController {

    private var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        let config = WKWebViewConfiguration()
        webView = WKWebView(frame: view.bounds)
        view.addSubview(webView)
        let urlString1 = "https://www.baidu.com"
        let urlString = "https://act.17laihou.com/page_rocket"
        webView.load(urlString)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        
//        getIPAddress(domainName: urlString)
        getIPAddress(domainName: urlString1)
    }
    
    
    // 域名解析

        func getIPAddress(domainName: String) -> String {
        var result = ""
            let host = CFHostCreateWithName(nil,domainName as CFString).takeRetainedValue()
            CFHostStartInfoResolution(host, .addresses, nil)
            var success: DarwinBoolean = false
            if let addresses = CFHostGetAddressing(host, &success)?.takeUnretainedValue() as NSArray?,
                let theAddress = addresses.firstObject as? NSData {
                var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
                if getnameinfo(theAddress.bytes.assumingMemoryBound(to: sockaddr.self), socklen_t(theAddress.length),
                               &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
                    let numAddress = String(cString: hostname)
                    result = numAddress
                    print(numAddress)
                }
            }
            return result
        }

}

extension WKWebView_Demo1VC: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("\(#function)")
    }
    
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        print("\(#function)")
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("\(#function)")
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print("\(#function), error: \(error)")
    }
}

extension WKWebView_Demo1VC: WKUIDelegate {
    
}
